import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

from driver import get_driver
from config import ua
from script_click_buttons import script_tap


name_list = []
url_list = []
number_list = []


def get_info(urls, uc, url_count=0):
    while True:
        for url in urls:
            url_count += 1
            browser = True
            while browser:
                scroll = 0
                print(url_count)
                try:
                    driver = get_driver(url, ua)
                    driver.find_element(By.XPATH, '//button[@class="close-button _color_black'
                                                  ' _circle _offset_small"]').\
                        click()
                    time.sleep(2)
                    name = driver.find_element(By.XPATH, '//h1[@class="card-title-view__title"]').text
                    number_phone = driver.find_element(By.XPATH, "//div[@class='card-phones-view__phone-number']").\
                        find_element(By.TAG_NAME, 'a').text
                    href = driver.find_element(By.XPATH, '//div[@class="business-urls-view__url"]').\
                        find_element(By.TAG_NAME, 'a').get_attribute('href')

                    print(f'{name}   {number_phone}   {href}\n URL NUMBER = {uc}')
                    time.sleep(1)
                    driver.find_element(By.XPATH, '//span[@class="business-header-rating-view__text'
                                                  ' _clickable"]').click()
                    driver.find_elements(By.XPATH, '//div[@class="business-reviews-card-view__ranking"]')
                    print('script')
                    script_tap()
                    time.sleep(4)
                    while True:
                        scroll += 1
                        print(f'scroll {scroll}')
                        time.sleep(2)
                        end = driver.find_elements(By.XPATH, '//div[@class="business-tab-wrapper"]')
                        ActionChains(driver).move_to_element(end[-1]).perform()
                        if scroll == 10:
                            time.sleep(1)
                            dates = driver.find_elements(By.XPATH, '//span[@class="business-review-view__date"]')
                            date = dates[-1].text
                            date = int(date[-4:])
                            print(date)
                            if date >= 2021:
                                time.sleep(1)
                                name_list.append(name)
                                url_list.append(href)
                                number_list.append(number_phone)
                                print(name_list, url_list, number_list)
                                if url_count == uc:
                                    print('ALL LINK PASSED WRITE DATES TO FILE')
                                    return {
                                        'Название': name_list,
                                        'Ссылка': url_list,
                                        'Контакты': number_list
                                        }
                                else:
                                    driver.close()
                                    break

                            else:
                                print('THIS DATES NOT NEED | SKIP')
                                browser = False
                                break

                        else:
                            continue

                except ConnectionAbortedError as ex:
                    print(f'{ex}try else')
                    continue
